home *** CD-ROM | disk | FTP | other *** search
- Path: colossus.holonet.net!russell
- From: russell@news.mdli.com (Russell Blackadar)
- Newsgroups: comp.lang.c++
- Subject: Re: how to pass matrix to function turbo c++?
- Date: 26 Jan 1996 02:21:41 GMT
- Organization: HoloNet National Internet Access System: 510-704-1058/modem
- Message-ID: <4e9drl$p87@colossus.holonet.net>
- References: <4e7ftm$11n@newsbf02.news.aol.com>
- NNTP-Posting-Host: jubal.mdli.com
- X-Newsreader: TIN [version 1.2 PL2]
-
- OTDance (otdance@aol.com) wrote:
- : I make the matrix - no problem in main. . .
-
- : I look for ways to pas the matrix to a function. . .big problems
- ...
-
- From your code, I'll assume you have an 8x5 array of char. If
- not, you can always adjust my answer to fit your case. Here's
- the simple answer:
-
- void func(char array[][5]) { // leave the first [] empty
- cout << array[2][3]; // ...or whatever
- }
- int main() {
- char array[8][5] = { "abcd", "efgh", "ijkl" /* etc... */ };
- func(array); // prints ^ this
- }
-
- Note that a 2-d array in C (or C++) is simply an array of arrays.
- The rule for passing any array as argument is: it gets passed via
- a pointer to its first element. In this case, the first element
- is a char[5] array, so yes, I could have written char (*array)[5]
- instead of char array[][5], in the argument list.
-
- BTW, I did not try to make the above const correct, or to use
- const int parameters instead of literal numbers -- both good
- habits to get into.
- --
- Russell Blackadar, russell@mdli.com
-